<%@ page contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script type="text/javascript">
$(function(){
	$('#admdivTree').tree({   
		url:'<c:url value="/system/admdiv/doGetAdmdiv.do"/>',
		onBeforeExpand: function(node) {
			$('#admdivTree').tree('options').url  = '<c:url value="/system/admdiv/doGetAdmdiv.do"/>?parentcode=' + node.id;
		},
		onLoadSuccess: function(node, data) {
			if($('#admdivTree').tree('getRoot')!=null){
				$('#admdivTree').tree('expand', $('#admdivTree').tree('getRoot').target);
			}
		},
		onClick: function(node) {
			$('#admdivCenter').panel('setTitle','行政区划管理-'+ node.text);		
			$('#tblListAdmdiv').datagrid('getPager').pagination({pageNumber:1});
			var queryParams = {parentcode:''};
			queryParams.parentcode = node.id;
			$('#tblListAdmdiv').datagrid('options').queryParams = queryParams;
			$('#tblListAdmdiv').datagrid('clearSelections').datagrid('reload');
			resetAdmdivQuery();
		}
	});

	$('#tblListAdmdiv').datagrid({
		title: '列表区',
		fit: true,
		nowrap: false,
		striped: true,
		url: '<c:url value="/system/admdiv/jsonAdmdivPageList.do"/>',
		queryParams:{parentcode:'0'},
		sortName: 'adcode',
		sortOrder: 'asc',
		remoteSort: true,
		idField: 'id',
		columns: [[
			{field:'adname',title:'区划名称',width:400,sortable:true},
			{field:'adcode',title:'区划编码',width:200,sortable:true}
		]],
		toolbar:[{
			id: 'admdivAdd',
			text:'新增',
			iconCls:'icon-add',
			handler:function(){
				addAdmdiv();
		}},{
			id: 'admdivEdit',
			text:'修改',
			iconCls:'icon-edit',
			handler:function(){
				editAdmdiv();
		}},{
			id: 'dictAdmdiv',
			text:'删除',
			iconCls:'icon-remove',
			handler:function(){
				delAdmdiv();
		}}],
		pageSize: 20,
		pagination: true,
		rownumbers: true,
		onClickRow:function(rowIndex, rowData) {
			$('#tblListAdmdiv').datagrid('clearSelections').datagrid('selectRow',rowIndex);	
		}
	});
});

function addAdmdiv(){
	var parentcode;
	if($('#admdivTree').tree('getSelected')!=null){
		parentcode = $('#admdivTree').tree('getSelected').id;
	}else{
		parentcode = $('#admdivTree').tree('getRoot').id;
	}
	$('#dlgAdmdiv').dialog('close').remove();
	$('body').append('<div id="dlgAdmdiv" class="easyui-dialog" closed="true"></div>');
	$('#dlgAdmdiv').dialog({
		title:'行政区划添加',
		modal:true,
		singleSelect:true,
		closable:true,
		draggable:false,
		width:350,
		height:160,
		toolbar:[{
			text:'保存',
			iconCls:'icon-save',
			handler:function(){
				saveAdmdiv();
			}
		},{
			text:'关闭',
			iconCls:'icon-cancel',
			handler:function(){
				$('#dlgAdmdiv').dialog('close');
			}
		}]
	}).dialog('open').dialog('refresh','<c:url value="/system/admdiv/admdivAdd.do"/>?parentcode='+parentcode);	
}

function editAdmdiv(){
	var rows = $('#tblListAdmdiv').datagrid('getSelected');
	if(rows==null){
		$.messager.alert('消息', '没有选中任何行政区划！', 'info');
	}else{
		var id = $('#tblListAdmdiv').datagrid('getSelected').id;
		$('#dlgAdmdiv').dialog('close').remove();
		$('body').append('<div id="dlgAdmdiv" class="easyui-dialog" closed="true"></div>');
		$('#dlgAdmdiv').dialog({
			title:'行政区划修改',
			modal:true,
			singleSelect:true,
			closable:true,
			draggable:false,
			width:350,
			height:160,
			toolbar:[{
				text:'保存',
				iconCls:'icon-save',
				handler:function(){
					saveAdmdiv();
				}
			},{
				text:'关闭',
				iconCls:'icon-cancel',
				handler:function(){
					$('#dlgAdmdiv').dialog('close');
				}
			}]
		}).dialog('open').dialog('refresh','<c:url value="/system/admdiv/admdivAdd.do"/>?id='+id);
	}
}

function delAdmdiv(){
	var rows = $('#tblListAdmdiv').datagrid('getSelected');
	if(rows==null){
		$.messager.alert('消息', '没有选中任何行政区划！', 'info');
	}else{
		$.messager.confirm('删除数据', '确定要删除所选择的数据？', function(r){
			if (r){
				$.ajax({
					url: "<c:url value='/system/admdiv/doDelAdmdiv.do' />",
					type: 'POST',
					data: 'id='+rows.id,
					beforeSend: function(XmlHttpRequest, textStatus, errorThrown) {
						$.messager.progress({
							title:'提示',
							msg:'正在执行数据操作，请稍待...'
						});
					},
					success: function(result) {
						$.messager.progress('close');
						if(!result.success) {
							$.messager.alert('消息', result.data, 'error');
						}
						else {
							$.messager.alert('提示', result.data , 'info');
							var node = $('#admdivTree').tree('getSelected');  //组织节点须选中
							if (node == null){
								node = $('#admdivTree').tree('getRoot');
							}
							$('#admdivTree').tree('getData',node.target).url = '<c:url value="/system/admdiv/doGetAdmdiv.do"/>?parentcode=' + node.id;
							$('#admdivTree').tree('reload', node.target);
							$('#admdivTree').tree('expand', node.target);
							$('#tblListAdmdiv').datagrid('clearSelections').datagrid('reload');
						}
					},
					complete: function (XMLHttpRequest, textStatus) {
					}
				});
			}
		});	
	}
}

function admdivQuery(){
	$('#tblListAdmdiv').datagrid('getPager').pagination({pageNumber:1});
	var queryParams = $('#tblListAdmdiv').datagrid('options').queryParams;
	if(typeof(queryParams.parentcode)=="undefined")
		queryParams.parentcode = $('#admdivTree').tree('getRoot').id;
	queryParams.adname = $('#frmAdmdivQuery input[name="adname"]').val();	
	queryParams.adcode = $('#frmAdmdivQuery input[name="adcode"]').val();
	$('#tblListAdmdiv').datagrid('options').queryParams = queryParams;
	$('#tblListAdmdiv').datagrid('clearSelections').datagrid('reload'); 
}

function resetAdmdivQuery(){
	$('#frmAdmdivQuery input[name="adname"]').val('');	
	$('#frmAdmdivQuery input[name="adcode"]').val('');
}

</script>
<div id="admdivLayout" class="easyui-layout" fit="true">
	<div id="treePanel" region="west" split="true" border="true" title="区划树" style="width:190px;padding:0px;">
		<ul id="admdivTree"></ul>
	</div>
	<div id="admdivCenter" region="center" title="行政区划管理" style="padding:5px;">
	    <div class="easyui-layout" fit="true">
	    		<div region="north" title="查询" border="true" style="height:90px;overflow:hidden;">			
					<form id="frmAdmdivQuery" method="post" style="margin:0;padding:0">
					<table cellpadding="0" cellspacing="0"  style="margin:10px 0 0 10px;">
						<tr>
							<th style="width:10%;">区划编码：</th>
							<td style="width:25%;"><input type="text" id="adcode" name="adcode" class="easyui-validatebox text" style="width:200px;" /></td>
							<th style="width:10%;">区划名称：</th>
							<td style="width:20%;"><input type="text" id="adname" name="adname" class="easyui-validatebox text" style="width:200px;" /></td>
							<td style="width:35%;padding-left:10px;">
							<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-search" onclick="admdivQuery();">查询</a>
							<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-reload" onclick="resetAdmdivQuery();">重置</a>
							</td>
						</tr>	
						<tr>
							<td align="center" colspan="5"><font color=red>注：查询时选择左边区划树行政区划，可按条件查询此选中区划项下的区划</font></td>
						</tr>
					</table>
					</form>
				</div>
				<div region="center" border="false" style="overflow:hidden;padding-top:5px;">
					<table id="tblListAdmdiv"></table>
				</div>
	    	</div>	    
	</div>
</div>